package Threaddemo.thread_0917;

import java.util.concurrent.atomic.AtomicInteger;

public class demo7_0518 {
    //原子类的使用
    public static AtomicInteger count = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    // 等价于 count++
                    count.incrementAndGet();
                    // 等价于  ++count
                    count.getAndIncrement();
                    // 等价于 count--
                    count.decrementAndGet();
                    // 等价于 --count
                    count.getAndDecrement();
                    // 等价于 count += 5
                    count.addAndGet(5);
                    // 等价于 count -= 5
                    count.addAndGet(-5);
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    count.getAndIncrement();
                }
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(count);
    }
}
